package string;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:19
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution541 {

    public String reverseStr(String s, int k) {
        int head = 0;
        int end = head + 2 * k;
        char[] charArray = s.toCharArray();
        while (end < s.length()) {
            reverseString(charArray, head, head + k - 1);
            head = end;
            end += 2 * k;
        }
        int rest = s.length() - head;
        if (rest >= k) {
            reverseString(charArray, head, head + k - 1);
        } else {
            reverseString(charArray, head, s.length() - 1);
        }
        return new String(charArray);
    }

    public void reverseString(char[] s, int start, int end) {
        while (start < end) {
            char temp = s[start];
            s[start++] = s[end];
            s[end--] = temp;
        }
    }

}
